package atm.frame;

import atm.pojo.UserDO;
import atm.service.UserService;
import atm.service.impl.UserServiceImpl;
import atm.util.SqlUtil;
import atm.util.Util;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Login extends JPanel implements ActionListener {
    private UserService userService;
    //声明交互按钮
    private JButton loginBtn;

    //账号，密码框
    private JTextField accountText;
    private JPasswordField passwordText;

    //主函数属性名称
    private MainPage frame;

    private Icon loginButton = Util.getIcon("/atmimage/buttonLand.jpg");

    private int errorCount = 3;

    public Login(MainPage frame) {
        //必须写，不然会报空指针
        this.frame = frame;

        this.setLayout(null);

        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("登录");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        //有交互的登录
        loginBtn = new JButton(loginButton);
        loginBtn.setBounds(350, 330, 100, 50);
        this.add(loginBtn);
        loginBtn.addActionListener(this);

        //账号，密码框
        accountText = new JTextField();
        accountText.setBounds(300, 150, 200, 25);
        this.add(accountText);
        accountText.addActionListener(this);

        passwordText = new JPasswordField();
        passwordText.setBounds(300, 200, 200, 25);
        this.add(passwordText);
        passwordText.addActionListener(this);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        //声明接口，但要new一个接口的实现类
        userService = new UserServiceImpl();
//        if (e.getSource() == loginBtn) {
//            boolean flag = true;
//            for (int i = 0; i < frame.getUserarraylist().size(); i++) {
//                if (frame.getUserarraylist().get(i).getAccount().equals(accountText.getText()) && frame.getUserarraylist().get(i).getPassword().equals(passwordText.getText())) {
//                    JOptionPane.showMessageDialog(this, "登录成功");
//                    frame.switchPanel("menu");
//                    break;
//                } else {
//                    JOptionPane.showMessageDialog(this, "登录失败，还剩");
//                    flag = false;
//                }
//            }
//            if (!flag) {
//                count++;
//            }
//            if (count == 2) {
//                JOptionPane.showMessageDialog(this, "账号冻结");
//                System.exit(0);
//            }
//        }

        if (e.getSource() == loginBtn) {
            String account = accountText.getText();
            String pwd = String.valueOf(passwordText.getPassword());
//            ArrayList<User> users = frame.getUserarraylist();
//            for (int i = 0; i < users.size(); i++) {
//                if (users.get(i).getAccount().equals(account) && users.get(i).getPassword().equals(pwd)) {
//                    JOptionPane.showMessageDialog(this, "登录成功");
//                    frame.setIndex(i);
//                    frame.setUser(frame.getUserarraylist().get(i));
//                    frame.switchPanel("menu");
//                    accountText.setText("");
//                    passwordText.setText("");
//                    return;
//                }
//            }

//            SqlUtil db = new SqlUtil();
//            UserDO user = db.getByUsernameAndPwd(account, pwd);

            UserDO user = userService.getBYUsernameAndPwd(account,pwd);
            if (user != null) {
                frame.setUser(user);
                frame.switchPanel("menu");
                return;
            }

            errorCount--;
            if (errorCount == 0) {
                JOptionPane.showMessageDialog(this, "账号冻结");
                System.exit(0);
            }
            accountText.setText("");
            passwordText.setText("");
            JOptionPane.showMessageDialog(this, "登陆失败，还剩" + errorCount + "次机会");
        }

    }
}
